
// pages/detail/detail.js
wx.cloud.init()
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    li:["胸部CT","头部CT","肺部CT"],
    imgList: [],
    modalName:'',
    tip:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    this.setData({idx:e.id})
    console.log(e.id)
    let tip = {
      title:"",
      content:""
    }
    tip["title"] =  this.data.li[e.id]
    //头部
    if(e.id==1){
      tip["content"] = ' 本功能暂时支持预测是否有肿瘤'
    }
    if(e.id==0){
      tip["content"] = '本功能暂时支持预测腺癌，大细胞癌，鳞状细胞癌'
    }
    if(e.id==2){
      tip["content"] = '本功能暂时支持预测肺浑浊, 新冠,  病毒性肺炎'
    }
    wx.showModal({
      title:tip["title"],
      content:tip["content"],
      showCancel:false,
      confirmText:"我知道了"
    })
    this.setData({tip})
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  ChooseImage() {
    var that = this
    let type = this.data.li[this.data.idx]
    wx.chooseImage({
      count: 1, 
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], //从相册选择
      success: (res) => {
        wx.showLoading({
          title: '正在上传中',
          mask:true
        })
        const tempFilePaths = res.tempFilePaths[0]
        console.log("选择成功",res)
        wx.cloud.uploadFile({
          cloudPath:type+'/' +new Date().getTime() + "_" +  Math.floor(Math.random()*1000) + ".png",
          filePath:tempFilePaths,// 本地文件路径
          success: res => {
            console.log(res)
            console.log(res.fileID)
            that.setData({fileID:res.fileID})
            wx.showToast({
              title: '上传成功',
              icon:"none"
            })
           

          }
        })
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    console.log(2)
    wx.showModal({
      title: "提示",
      content: '确定要删除吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList: this.data.imgList
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  submit(){
    if(this.data.imgList.length==0){
      wx.showToast({
        title: '您还没上传图片',
        icon:"none"
      })
      return -1
    }
    console.log(2)
    let type = {}
    type["name"]= this.data.li[this.data.idx]
    var that = this
    if(type["name"]=="胸部CT"){
      type["maxAge"] = "350 * 350"
      type["url"] = "http://175.178.190.131:88/pre_chest_ct"
    }
    if(type["name"]=="头部CT"){
      type["maxAge"] = "224 * 224"
      type["url"] = "http://175.178.190.131:88/pre_brain_MRI"
    }    
    if(type["name"]=="肺部CT"){
      type["maxAge"] = "224 * 224"
      type["url"] = "http://175.178.190.131:88/pre_covid_CT"
    }
    console.log(that.data.fileID)
    console.log(type["maxAge"])
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.getTempFileURL({
      fileList: [{
        fileID: that.data.fileID,
        // maxAge:  type["maxAge"], // one hour
      }]
    }).then(res => {
      console.log(res)
      let url = res.fileList[0].tempFileURL
      console.log(url)
      console.log(wx.getStorageSync('openid'))
      wx.request({
        url: type["url"],
        data:{
          open_id:wx.getStorageSync('openid'),
          url:url
        },
        method:"POST",
        success(res){
          if(res.data.code=="200"){
            wx.showToast({
              title: '成功',
              icon:"none"
            })
            console.log(res)
            that.setData({modalName:"Modal",state:res.data.result.state,pre:res.data.result.pre})
            db.collection("history_log").add({
              data:{
                openid:wx.getStorageSync('openid'),
                image_url:url,
                time:new Date().getTime(),
                type:type["name"],
                state:res.data.result.state,
                pre:res.data.result.pre,
         
              }
            })
          }
         else{
           wx.showToast({
             title: '请联系客服',
             icon:"none"
           })
         }
        }
      })
      // console.log(res.fileList)
    }).catch(error => {
      console.log(error)
      // handle error
    })
  },
  look_detail(){
    
    wx.navigateTo({
      url: '/pages/state_detail/state_detail?state='+this.data.state,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})